<?php

//include_once "CF_all_com.php";

// funciton to create a drop-down menu from an array of arrays, reflecting results of a quwery.
// $valuesarray is the array, formed by putting each mysql_fetch_array into a holder array
// $selectname is the name of the select box created
// $showkey is the key (string) of the string to put int he drop-down to have show up
// $valuekey is the key (string) of the data to put in the value = part
// $selectedvalue is the key (string) of the value to show as selected when form is created.
// $query it the query to get the text from
// $text limit is the max length of option text
// CT
//CT - 011308 - Added get_mysql_array funciton
// ct-010408 - Added do_query function
// ct-2008-01-15 - Added check_for_data function
// ct 2008-03-08 - Added DEBUGlookup_value, commented out 'no results for autotable query' in make_table 


// ----------------------------------------------------------------Support Functions

//This function can be used to return a value from a table for use .
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function lookup_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT $res_column FROM $tablestring WHERE $whereclause LIMIT 1";
	$result = mysql_query($query_lookup) or ($value = "Error");
	//print $query_lookup . "\n" . mysql_error() . "\n";
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row[$res_column];
		}
	return $value;

}

function DEBUGlookup_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT $res_column FROM $tablestring WHERE $whereclause LIMIT 1";
	$result = mysql_query($query_lookup) or ($value = "Error");
	print $query_lookup . "\n" . mysql_error() . "\n";
	exit;
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row[$res_column];
		}
	return $value;

}

//This function can be used to return the maximum value (usefull for inserts when we cant do it all in one insert)
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_max_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT MAX($res_column) FROM $tablestring WHERE $whereclause LIMIT 1";
	$result = mysql_query($query_lookup) or ($value = "Error");
	//print $query_lookup . "  " . $value . "\n";
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row["MAX(".$res_column.")"];
		//$value=$row[1];
		}
		//print "\n Value is : $value";
	return $value;

}

//This function can be used to return the maximum value (usefull for inserts when we cant do it all in one insert)
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_query_count ($thequery) {
	$thequery = str_replace("\n", " ", $thequery);
		
	$thequery = preg_replace("/SELECT.+FROM/", "SELECT count(*) FROM", $thequery);

	$result = mysql_query($thequery) or ($value = 0);
	//print $query_lookup . "  " . $value . "\n";
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row["count(*)"];
		//$value=$row[1];
		}
		//print "\n Value is : $value";
	return $value;

}


//This function can be used to return a delimited list of values from a query.
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_value_list($query_lookup, $delimiter) {
	$get_results = mysql_query($query_lookup) or die(mysql_error());

	if(mysql_num_rows($get_results)) {
		$index = 0;
		while($resultsarray = mysql_fetch_array($get_results)) {
			if($index > 0) {
				$value .= $delimiter;
				}
			$value .=  $resultsarray[0] ;
			$index ++;
			}
		}
	
	
	return $value;

}


// This function checks if a value exist or not in the DB, returns 1 on data and 0 of empty set.
function check_for_data($query_lookup){
	$get_results = mysql_query($query_lookup) or die(mysql_error());

	if(mysql_num_rows($get_results)) {
		return 1;
		}
	else {
		return 0;
		}

}

function get_mysql_array($thequery){
	$get_results = mysql_query($thequery) or die(mysql_error());
	
	if(mysql_num_rows($get_results)) {
		$index = 0;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}
		return $valuesarray;
		}
}

//GENERIC WRAPPER FUNCTION for Query Exec
function do_query($query) {
	$result = mysql_query($query) or die(mysql_error());
	return $result;
}

function get_array_query($thequery){
	$get_results = mysql_query($thequery) or die(mysql_error());
	
	if(mysql_num_rows($get_results)) {
		$index = 0;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}
		return $valuesarray;
		}

}

function get_array_limitquery($thequery, $start = 0, $limit = 'MAX'){
	if($limit == 'MAX'){
		$get_results = mysql_query($thequery) or die(mysql_error());
	}else{
		$get_results = mysql_query($thequery . " LIMIT $start, $limit ") or die(mysql_error());
	}
	if(mysql_num_rows($get_results)) {
		$index = 1;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}
		return $valuesarray;
		}

}

?>
